
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>查询 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../specifics/plug.html" />
    
    
    <link rel="prev" href="associations.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../basics/basics.html">
            
                <a href="../basics/basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../basics/collections.html">
            
                <a href="../basics/collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../basics/enum.html">
            
                <a href="../basics/enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../basics/pattern-matching.html">
            
                <a href="../basics/pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../basics/control-structures.html">
            
                <a href="../basics/control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../basics/functions.html">
            
                <a href="../basics/functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../basics/pipe-operator.html">
            
                <a href="../basics/pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../basics/modules.html">
            
                <a href="../basics/modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../basics/mix.html">
            
                <a href="../basics/mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../basics/sigils.html">
            
                <a href="../basics/sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../basics/documentation.html">
            
                <a href="../basics/documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../basics/testing.html">
            
                <a href="../basics/testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../basics/comprehensions.html">
            
                <a href="../basics/comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../basics/strings.html">
            
                <a href="../basics/strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../basics/date-time.html">
            
                <a href="../basics/date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../basics/mix-tasks.html">
            
                <a href="../basics/mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../basics/iex-helpers.html">
            
                <a href="../basics/iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../advanced/erlang.html">
            
                <a href="../advanced/erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../advanced/error-handling.html">
            
                <a href="../advanced/error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../advanced/escripts.html">
            
                <a href="../advanced/escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../advanced/concurrency.html">
            
                <a href="../advanced/concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../advanced/otp-concurrency.html">
            
                <a href="../advanced/otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../advanced/otp-supervisors.html">
            
                <a href="../advanced/otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../advanced/otp-distribution.html">
            
                <a href="../advanced/otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../advanced/metaprogramming.html">
            
                <a href="../advanced/metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../advanced/umbrella-projects.html">
            
                <a href="../advanced/umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../advanced/typespec.html">
            
                <a href="../advanced/typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="../advanced/behaviours.html">
            
                <a href="../advanced/behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="../advanced/gen-stage.html">
            
                <a href="../advanced/gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="../advanced/protocols.html">
            
                <a href="../advanced/protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="../advanced/nerves.html">
            
                <a href="../advanced/nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="basics.html">
            
                <a href="basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="changesets.html">
            
                <a href="changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="associations.html">
            
                <a href="associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="3.4" data-path="querying.html">
            
                <a href="querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >查询</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>&#x672C;&#x7AE0;&#x8282;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x57FA;&#x4E8E;<a href="associations">&#x524D;&#x9762;&#x8BFE;&#x7A0B;</a>&#x5B8C;&#x6210;&#x7684;&#x7535;&#x5F71;&#x5206;&#x7C7B;&#x9886;&#x57DF;&#x505A;&#x7684; <code>Example</code> &#x5E94;&#x7528;&#x7EE7;&#x7EED;&#x6211;&#x4EEC;&#x7684;&#x8BFE;&#x7A0B;&#x548C;&#x5B9E;&#x8DF5;&#x3002;</p>
<h2 id="&#x4F7F;&#x7528;-ectorepo-&#x6765;&#x83B7;&#x53D6;&#x8BB0;&#x5F55;">&#x4F7F;&#x7528; <code>Ecto.Repo</code> &#x6765;&#x83B7;&#x53D6;&#x8BB0;&#x5F55;</h2>
<p>&#x5728; Ecto &#x91CC;&#x9762;&#xFF0C;&#x4E00;&#x4E2A; &#x201C;repository&#x201D; &#x5BF9;&#x5E94;&#x4E86;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x5B58;&#x50A8;&#x7A7A;&#x95F4;&#xFF0C;&#x6BD4;&#x5982;&#x6211;&#x4EEC;&#x7684; Postgres &#x6570;&#x636E;&#x5E93;&#x3002;&#x6240;&#x6709;&#x548C;&#x6B64;&#x6570;&#x636E;&#x5E93;&#x6253;&#x4EA4;&#x9053;&#x7684;&#x64CD;&#x4F5C;&#x90FD;&#x662F;&#x7ECF;&#x8FC7;&#x8FD9;&#x4E2A; repository &#x6765;&#x5B9E;&#x73B0;&#x7684;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x597D;&#x4E00;&#x4E9B;&#x51FD;&#x6570;&#x7684;&#x5E2E;&#x52A9;&#xFF0C;&#x76F4;&#x63A5;&#x5BF9; <code>Example.Repo</code> &#x8FDB;&#x884C;&#x6570;&#x636E;&#x5E93;&#x67E5;&#x8BE2;&#x3002;</p>
<h3 id="&#x901A;&#x8FC7;-id-&#x83B7;&#x53D6;&#x8BB0;&#x5F55;">&#x901A;&#x8FC7; ID &#x83B7;&#x53D6;&#x8BB0;&#x5F55;</h3>
<p>&#x7ED9;&#x5B9A;&#x6570;&#x636E;&#x8BB0;&#x5F55; ID &#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>Repo.get/3</code> &#x51FD;&#x6570;&#x6765;&#x4ECE;&#x6570;&#x636E;&#x5E93;&#x83B7;&#x53D6;&#x8BB0;&#x5F55;&#x3002;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x9700;&#x8981;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;&#x4E00;&#x4E2A;&#x201C;&#x53EF;&#x67E5;&#x8BE2;&#x201D;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#xFF0C;&#x548C;&#x8981;&#x4ECE;&#x6570;&#x636E;&#x83B7;&#x53D6;&#x7684;&#x8BB0;&#x5F55;&#x7684; ID&#x3002;&#x5982;&#x679C; ID &#x5BF9;&#x5E94;&#x7684;&#x8BB0;&#x5F55;&#x5B58;&#x5728;&#xFF0C;&#x5B83;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x63CF;&#x8FF0;&#x4E86;&#x8BB0;&#x5F55;&#x8BE6;&#x60C5;&#x7684;&#x7ED3;&#x6784;&#x4F53;&#x3002;&#x5982;&#x679C;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x8FD4;&#x56DE; <code>nil</code>&#x3002;</p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x770B;&#x770B;&#x4E0B;&#x9762;&#x5229;&#x7528;&#x7535;&#x5F71; ID &#x83B7;&#x53D6;&#x8BB0;&#x5F55; ID &#x503C;&#x4E3A; 1 &#x7684;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; <span class="hljs-keyword">alias</span> Example.{Repo, Movie}
iex&gt; Repo.get(Movie, <span class="hljs-number">1</span>)
%Example.Movie{
  <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;movies&quot;</span>&gt;,
  <span class="hljs-symbol">actors:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:actors</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">characters:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:characters</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">distributor:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:distributor</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
  <span class="hljs-symbol">tagline:</span> <span class="hljs-string">&quot;Something about video games&quot;</span>,
  <span class="hljs-symbol">title:</span> <span class="hljs-string">&quot;Ready Player One&quot;</span>
}
</code></pre>
<p>&#x53EF;&#x89C1;&#x6211;&#x4EEC;&#x4F20;&#x5165; <code>Repo.get/3</code> &#x51FD;&#x6570;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F; <code>Movie</code> &#x6A21;&#x5757;&#x3002;<code>Movie</code> &#x662F;&#x4E00;&#x4E2A;&#x201C;&#x53EF;&#x67E5;&#x8BE2;&#x201D;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x4F7F;&#x7528;&#x4E86; <code>Ecto.Schema</code> &#x6A21;&#x5757;&#x5E76;&#x4E14;&#x6839;&#x636E;&#x5B83;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A; schema&#x3002;&#x8FD9;&#x4F7F;&#x5F97; <code>Movie</code> &#x62E5;&#x6709;&#x4E86; <code>Ecto.Queryable</code> &#x534F;&#x8BAE;&#x3002;&#x8FD9;&#x4E2A;&#x534F;&#x8BAE;&#x628A;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x8F6C;&#x6362;&#x6210; <code>Ecto.Query</code>&#x3002;Ecto &#x67E5;&#x8BE2;&#x5BF9;&#x8C61;&#x7684;&#x4F5C;&#x7528;&#x5C31;&#x662F;&#x4ECE; repository &#x4E2D;&#x83B7;&#x53D6;&#x6570;&#x636E;&#x3002;&#x540E;&#x9762;&#x4F1A;&#x6709;&#x66F4;&#x591A;&#x5173;&#x4E8E;&#x67E5;&#x8BE2;&#x5BF9;&#x8C61;&#x7684;&#x4ECB;&#x7ECD;&#x3002;</p>
<h3 id="&#x6839;&#x636E;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;&#x503C;&#x83B7;&#x53D6;&#x8BB0;&#x5F55;">&#x6839;&#x636E;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;&#x503C;&#x83B7;&#x53D6;&#x8BB0;&#x5F55;</h3>
<p>&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>Repo.get_by/3</code> &#x51FD;&#x6570;&#x6765;&#x83B7;&#x53D6;&#x6EE1;&#x8DB3;&#x4E00;&#x5B9A;&#x6761;&#x4EF6;&#x7684;&#x6570;&#x636E;&#x3002;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x9700;&#x8981;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;&#x4E00;&#x4E2A;&#x201C;&#x53EF;&#x67E5;&#x8BE2;&#x201D;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#xFF0C;&#x548C;&#x6211;&#x4EEC;&#x67E5;&#x8BE2;&#x7684;&#x6761;&#x4EF6;&#x3002;<code>Repo.get_by/3</code> &#x8FD4;&#x56DE;&#x7684;&#x662F;&#x5355;&#x6761;&#x8BB0;&#x5F55;&#x3002;&#x8BF7;&#x770B;&#x4EE5;&#x4E0B;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; <span class="hljs-keyword">alias</span> Example.Repo
iex&gt; <span class="hljs-keyword">alias</span> Example.Movie
iex&gt; Repo.get_by(Movie, <span class="hljs-symbol">title:</span> <span class="hljs-string">&quot;Ready Player One&quot;</span>)
%Example.Movie{
  <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;movies&quot;</span>&gt;,
  <span class="hljs-symbol">actors:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:actors</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">characters:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:characters</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">distributor:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:distributor</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
  <span class="hljs-symbol">tagline:</span> <span class="hljs-string">&quot;Something about video games&quot;</span>,
  <span class="hljs-symbol">title:</span> <span class="hljs-string">&quot;Ready Player One&quot;</span>
}
</code></pre>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x7F16;&#x5199;&#x66F4;&#x590D;&#x6742;&#x7684;&#x67E5;&#x8BE2;&#xFF0C;&#x6216;&#x8005;&#x83B7;&#x5F97; <em>&#x6240;&#x6709;&#x7684;</em> &#x6EE1;&#x8DB3;&#x6761;&#x4EF6;&#x7684;&#x8BB0;&#x5F55;&#xFF0C;&#x6211;&#x4EEC;&#x5219;&#x9700;&#x8981;&#x4F7F;&#x7528; <code>Ecto.Query</code> &#x6A21;&#x5757;&#x3002;</p>
<h2 id="&#x4F7F;&#x7528;-ectoquery-&#x7F16;&#x5199;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;">&#x4F7F;&#x7528; <code>Ecto.Query</code> &#x7F16;&#x5199;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;</h2>
<p><code>Ecto.Query</code> &#x6A21;&#x5757;&#x63D0;&#x4F9B;&#x4E86;&#x67E5;&#x8BE2; DSL&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5B83;&#x6765;&#x4ECE;&#x5E94;&#x7528;&#x7684; repository &#x67E5;&#x8BE2;&#x548C;&#x83B7;&#x53D6;&#x6570;&#x636E;&#x3002;</p>
<h3 id="&#x901A;&#x8FC7;-ectoqueryfrom2-&#x521B;&#x5EFA;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;">&#x901A;&#x8FC7; <code>Ecto.Query.from/2</code> &#x521B;&#x5EFA;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;</h3>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>Ecto.Query.from/2</code> &#x51FD;&#x6570;&#x6765;&#x521B;&#x5EFA;&#x67E5;&#x8BE2;&#x3002;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x63A5;&#x6536;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;&#x4E00;&#x4E2A;&#x662F;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x548C;&#x4E00;&#x4E2A;&#x5173;&#x952E;&#x5B57;&#x5217;&#x8868;&#x3002;&#x8BA9;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x4ECE; repository &#x83B7;&#x53D6;&#x6240;&#x6709;&#x7684;&#x7535;&#x5F71;&#x7684;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#xFF1A;</p>
<pre><code class="lang-elixir">import Ecto.Query
query = from(m <span class="hljs-keyword">in</span> Movie, <span class="hljs-symbol">select:</span> m)
<span class="hljs-comment">#Ecto.Query&lt;from m in Example.Movie, select: m&gt;</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>Repo.all/2</code> &#x51FD;&#x6570;&#x6765;&#x6267;&#x884C;&#x8FD9;&#x4E2A;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x3002;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x5FC5;&#x987B;&#x63A5;&#x6536;&#x4E00;&#x4E2A; Ecto &#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x53C2;&#x6570;&#xFF0C;&#x5175;&#x89E3;&#x8FD4;&#x56DE;&#x6EE1;&#x8DB3;&#x67E5;&#x8BE2;&#x6761;&#x4EF6;&#x7684;&#x6240;&#x6709;&#x8BB0;&#x5F55;&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; Repo.all(query)

<span class="hljs-number">14</span><span class="hljs-symbol">:</span><span class="hljs-number">58</span><span class="hljs-symbol">:</span><span class="hljs-number">03</span>.<span class="hljs-number">187</span> [debug] QUERY OK source=<span class="hljs-string">&quot;movies&quot;</span> db=<span class="hljs-number">1.7</span>ms decode=<span class="hljs-number">4.2</span>ms
[
  %Example.Movie{
    <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;movies&quot;</span>&gt;,
    <span class="hljs-symbol">actors:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:actors</span> is <span class="hljs-keyword">not</span> loaded&gt;,
    <span class="hljs-symbol">characters:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:characters</span> is <span class="hljs-keyword">not</span> loaded&gt;,
    <span class="hljs-symbol">distributor:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:distributor</span> is <span class="hljs-keyword">not</span> loaded&gt;,
    <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
    <span class="hljs-symbol">tagline:</span> <span class="hljs-string">&quot;Something about video games&quot;</span>,
    <span class="hljs-symbol">title:</span> <span class="hljs-string">&quot;Ready Player One&quot;</span>
  }
]
</code></pre>
<h4 id="&#x4F7F;&#x7528;-from-&#x6784;&#x5EFA;&#x5173;&#x952E;&#x5B57;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;">&#x4F7F;&#x7528; <code>from</code> &#x6784;&#x5EFA;&#x5173;&#x952E;&#x5B57;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;</h4>
<p>&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x7ED9; <code>from/2</code> &#x4F20;&#x5165;&#x4E00;&#x4E2A; <em>&#x5173;&#x952E;&#x5B57;&#x67E5;&#x8BE2;</em> &#x53C2;&#x6570;&#x6765;&#x6784;&#x9020;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x3002;&#x5F53;&#x4F7F;&#x7528; <code>from</code> &#x6765;&#x6784;&#x9020;&#x5173;&#x952E;&#x5B57;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x662F;&#xFF0C;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x53EF;&#x4EE5;&#x662F;&#x4EE5;&#x4E0B;&#x4E24;&#x79CD;&#x60C5;&#x51B5;&#x7684;&#x4EFB;&#x4E00;&#x79CD;&#xFF1A;</p>
<ul>
<li>&#x4E00;&#x4E2A; <code>in</code> &#x8868;&#x8FBE;&#x5F0F;&#xFF08;&#x6BD4;&#x5982;&#xFF1A;<code>m in Movie</code>&#xFF09;  </li>
<li>&#x4E00;&#x4E2A;&#x5B9E;&#x73B0;&#x4E86; <code>Ecto.Queryable</code> &#x534F;&#x8BAE;&#x7684;&#x6A21;&#x5757;&#xFF08;&#x6BD4;&#x5982;&#xFF1A;<code>Movie</code>&#xFF09;  </li>
</ul>
<p>&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x5219;&#x662F;&#x6211;&#x4EEC;&#x7684; <code>select</code> &#x5173;&#x952E;&#x5B57;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x3002;</p>
<h4 id="&#x4F7F;&#x7528;-from-&#x6784;&#x5EFA;&#x67E5;&#x8BE2;&#x8868;&#x8FBE;&#x5F0F;">&#x4F7F;&#x7528; <code>from</code> &#x6784;&#x5EFA;&#x67E5;&#x8BE2;&#x8868;&#x8FBE;&#x5F0F;</h4>
<p>&#x5F53;&#x628A; <code>from</code> &#x4F7F;&#x7528;&#x4E8E;&#x67E5;&#x8BE2;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x5FC5;&#x987B;&#x662F;&#x5B9E;&#x73B0;&#x4E86; <code>Ecto.Queryable</code> &#x534F;&#x8BAE;&#x7684;&#x6A21;&#x5757;&#xFF08;&#x6BD4;&#x5982;&#xFF1A;<code>Movie</code>&#xFF09;&#x3002;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x5219;&#x662F;&#x4E00;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x3002;&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; query = select(Movie, [m], m)
%Ecto.Query&lt;from m <span class="hljs-keyword">in</span> Example.Movie, <span class="hljs-symbol">select:</span> m&gt;
iex&gt; Repo.all(query)

<span class="hljs-number">06</span><span class="hljs-symbol">:</span><span class="hljs-number">16</span><span class="hljs-symbol">:</span><span class="hljs-number">20.854</span> [debug] QUERY OK source=<span class="hljs-string">&quot;movies&quot;</span> db=<span class="hljs-number">0</span>.<span class="hljs-number">9</span>ms
[
  %Example.Movie{
    <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;movies&quot;</span>&gt;,
    <span class="hljs-symbol">actors:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:actors</span> is <span class="hljs-keyword">not</span> loaded&gt;,
    <span class="hljs-symbol">characters:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:characters</span> is <span class="hljs-keyword">not</span> loaded&gt;,
    <span class="hljs-symbol">distributor:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:distributor</span> is <span class="hljs-keyword">not</span> loaded&gt;,
    <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
    <span class="hljs-symbol">tagline:</span> <span class="hljs-string">&quot;Something about video games&quot;</span>,
    <span class="hljs-symbol">title:</span> <span class="hljs-string">&quot;Ready Player One&quot;</span>
  }
]
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5728; <em>&#x4E0D;&#x9700;&#x8981;</em> &#x4E00;&#x4E2A; <code>in</code> &#x8BED;&#x53E5;&#xFF08;<code>m in Movie</code>&#xFF09;&#x7684;&#x65F6;&#x5019;&#x4F7F;&#x7528;&#x67E5;&#x8BE2;&#x8868;&#x8FBE;&#x5F0F;&#x3002;&#x5F53;&#x6211;&#x4EEC;&#x4E0D;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x67D0;&#x4E2A;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x7684;&#x5F15;&#x7528;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4E0D;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A; <code>in</code> &#x8BED;&#x53E5;&#x3002;&#x6211;&#x4EEC;&#x7684;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#xFF0C;&#x56E0;&#x4E3A;&#x4E0D;&#x9700;&#x8981;&#x8BF4;&#x6839;&#x636E;&#x67D0;&#x79CD;&#x7279;&#x5B9A;&#x7684;&#x6761;&#x4EF6;&#x6765;&#x9009;&#x62E9;&#x7535;&#x5F71;&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x9700;&#x8981;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x7684;&#x5F15;&#x7528;&#x3002;&#x90A3;&#x4E48;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x4E0D;&#x9700;&#x8981;&#x4F7F;&#x7528; <code>in</code> &#x8868;&#x8FBE;&#x5F0F;&#x548C;&#x5173;&#x952E;&#x5B57;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x3002;</p>
<h3 id="&#x4F7F;&#x7528;-select-&#x8868;&#x8FBE;&#x5F0F;">&#x4F7F;&#x7528; <code>select</code> &#x8868;&#x8FBE;&#x5F0F;</h3>
<p>&#x6211;&#x4EEC;&#x4F7F;&#x7528; <code>Ecto.Query.select/3</code> &#x51FD;&#x6570;&#x6765;&#x6307;&#x5B9A;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x4E2D;&#x7684; select &#x90E8;&#x5206;&#x3002;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x53EA;&#x60F3;&#x83B7;&#x53D6;&#x5176;&#x4E2D;&#x67D0;&#x4E9B;&#x5B57;&#x6BB5;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x539F;&#x5B50;&#x5217;&#x8868;&#x6765;&#x6307;&#x5B9A;&#x90A3;&#x4E9B;&#x5B57;&#x6BB5;&#xFF0C;&#x6216;&#x8005;&#x662F;&#x5F15;&#x7528;&#x7ED3;&#x6784;&#x4F53;&#x4E2D;&#x7684;&#x67D0;&#x4E9B;&#x952E;&#x3002;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x770B;&#x7B2C;&#x4E00;&#x79CD;&#x65B9;&#x5F0F;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; query = from(Movie, <span class="hljs-symbol">select:</span> [<span class="hljs-symbol">:title</span>])                                            
%Ecto.Query&lt;from m <span class="hljs-keyword">in</span> Example.Movie, <span class="hljs-symbol">select:</span> [<span class="hljs-symbol">:title</span>]&gt;
iex&gt; Repo.all(query)

<span class="hljs-number">15</span><span class="hljs-symbol">:</span><span class="hljs-number">15</span><span class="hljs-symbol">:</span><span class="hljs-number">25.842</span> [debug] QUERY OK source=<span class="hljs-string">&quot;movies&quot;</span> db=<span class="hljs-number">1.3</span>ms
[
  %Example.Movie{
    <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;movies&quot;</span>&gt;,
    <span class="hljs-symbol">actors:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:actors</span> is <span class="hljs-keyword">not</span> loaded&gt;,
    <span class="hljs-symbol">characters:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:characters</span> is <span class="hljs-keyword">not</span> loaded&gt;,
    <span class="hljs-symbol">distributor:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:distributor</span> is <span class="hljs-keyword">not</span> loaded&gt;,
    <span class="hljs-symbol">id:</span> <span class="hljs-keyword">nil</span>,
    <span class="hljs-symbol">tagline:</span> <span class="hljs-keyword">nil</span>,
    <span class="hljs-symbol">title:</span> <span class="hljs-string">&quot;Ready Player One&quot;</span>
  }
]
</code></pre>
<p>&#x8BF7;&#x7559;&#x610F;&#xFF0C;&#x6211;&#x4EEC;&#x5E76; <em>&#x6CA1;&#x6709;</em> &#x4F7F;&#x7528;&#x4E00;&#x4E2A; <code>in</code> &#x8868;&#x8FBE;&#x5F0F;&#x4F5C;&#x4E3A;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x4F20;&#x5165; <code>from</code> &#x51FD;&#x6570;&#x3002;&#x90A3;&#x65F6;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x6CA1;&#x6709;&#x5728; <code>select</code> &#x4E2D;&#x4F7F;&#x7528;&#x5173;&#x952E;&#x5B57;&#x5217;&#x8868;&#xFF0C;&#x6240;&#x4EE5;&#x5E76;&#x4E0D;&#x9700;&#x8981;&#x4E3A;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5F15;&#x7528;&#x3002;</p>
<p>&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x8FD4;&#x56DE;&#x7684;&#x7ED3;&#x6784;&#x4F53;&#xFF0C;&#x53EA;&#x5305;&#x542B;&#x4E86; <code>title</code> &#x952E;&#x503C;&#x3002;</p>
<p>&#x7B2C;&#x4E8C;&#x79CD;&#x65B9;&#x5F0F;&#x7684;&#x8868;&#x73B0;&#x884C;&#x4E3A;&#x5219;&#x6709;&#x70B9;&#x4E0D;&#x540C;&#x3002;&#x8FD9;&#x6B21;&#xFF0C;&#x6211;&#x4EEC; <em>&#x786E;&#x5B9E;</em> &#x9700;&#x8981;&#x4F7F;&#x7528;&#x4E00;&#x4E2A; <code>in</code> &#x8868;&#x8FBE;&#x5F0F;&#x3002;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x4E86;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x7684;&#x5F15;&#x7528;&#xFF0C;&#x6765;&#x6307;&#x660E;&#x7535;&#x5F71;&#x7ED3;&#x6784;&#x4F53;&#x4E2D;&#x7684; <code>title</code> &#x952E;&#xFF1A;</p>
<pre><code class="lang-elixir">iex(<span class="hljs-number">15</span>)&gt; query = from(m <span class="hljs-keyword">in</span> Movie, <span class="hljs-symbol">select:</span> m.title)   
%Ecto.Query&lt;from m <span class="hljs-keyword">in</span> Example.Movie, <span class="hljs-symbol">select:</span> m.title&gt;
iex(<span class="hljs-number">16</span>)&gt; Repo.all(query)                             

<span class="hljs-number">15</span><span class="hljs-symbol">:</span><span class="hljs-number">06</span><span class="hljs-symbol">:</span><span class="hljs-number">12.752</span> [debug] QUERY OK source=<span class="hljs-string">&quot;movies&quot;</span> db=<span class="hljs-number">4.5</span>ms queue=<span class="hljs-number">0</span>.<span class="hljs-number">1</span>ms
[<span class="hljs-string">&quot;Ready Player One&quot;</span>]
</code></pre>
<p>&#x4E0D;&#x540C;&#x7684;&#x662F;&#xFF0C;&#x8FD9;&#x79CD;&#x4F7F;&#x7528; <code>select</code> &#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x5305;&#x542B;&#x4E86;&#x6307;&#x5B9A;&#x503C;&#x7684;&#x5217;&#x8868;&#x3002;</p>
<h3 id="&#x4F7F;&#x7528;-where-&#x8868;&#x8FBE;&#x5F0F;">&#x4F7F;&#x7528; <code>where</code> &#x8868;&#x8FBE;&#x5F0F;</h3>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>where</code> &#x8868;&#x8FBE;&#x5F0F;&#x6765;&#x5305;&#x542B;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x7684; &#x201C;where&#x201D; &#x90E8;&#x5206;&#x3002;&#x591A;&#x4E2A; <code>where</code> &#x8868;&#x8FBE;&#x5F0F;&#x5219;&#x4F1A;&#x88AB;&#x5408;&#x6210;&#x4E3A; <code>WHERE AND</code> &#x7684; SQL &#x8BED;&#x53E5;&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; query = from(m <span class="hljs-keyword">in</span> Movie, <span class="hljs-symbol">where:</span> m.title == <span class="hljs-string">&quot;Ready Player One&quot;</span>)                   
%Ecto.Query&lt;from m <span class="hljs-keyword">in</span> Example.Movie, <span class="hljs-symbol">where:</span> m.title == <span class="hljs-string">&quot;Ready Player One&quot;</span>&gt;
iex&gt; Repo.all(query)

<span class="hljs-number">15</span><span class="hljs-symbol">:</span><span class="hljs-number">18</span><span class="hljs-symbol">:</span><span class="hljs-number">35.355</span> [debug] QUERY OK source=<span class="hljs-string">&quot;movies&quot;</span> db=<span class="hljs-number">4.1</span>ms queue=<span class="hljs-number">0</span>.<span class="hljs-number">1</span>ms
[
  %Example.Movie{
    <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;movies&quot;</span>&gt;,
    <span class="hljs-symbol">actors:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:actors</span> is <span class="hljs-keyword">not</span> loaded&gt;,
    <span class="hljs-symbol">characters:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:characters</span> is <span class="hljs-keyword">not</span> loaded&gt;,
    <span class="hljs-symbol">distributor:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:distributor</span> is <span class="hljs-keyword">not</span> loaded&gt;,
    <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
    <span class="hljs-symbol">tagline:</span> <span class="hljs-string">&quot;Something about video games&quot;</span>,
    <span class="hljs-symbol">title:</span> <span class="hljs-string">&quot;Ready Player One&quot;</span>
  }
]
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x4F7F;&#x7528; <code>where</code> &#x548C; <code>select</code>&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; query = from(m <span class="hljs-keyword">in</span> Movie, <span class="hljs-symbol">where:</span> m.title == <span class="hljs-string">&quot;Ready Player One&quot;</span>, <span class="hljs-symbol">select:</span> m.tagline)
%Ecto.Query&lt;from m <span class="hljs-keyword">in</span> Example.Movie, <span class="hljs-symbol">where:</span> m.title == <span class="hljs-string">&quot;Ready Player One&quot;</span>, <span class="hljs-symbol">select:</span> m.tagline&gt;
iex&gt; Repo.all(query)

<span class="hljs-number">15</span><span class="hljs-symbol">:</span><span class="hljs-number">19</span><span class="hljs-symbol">:</span><span class="hljs-number">11.904</span> [debug] QUERY OK source=<span class="hljs-string">&quot;movies&quot;</span> db=<span class="hljs-number">4.1</span>ms
[<span class="hljs-string">&quot;Something about video games&quot;</span>]
</code></pre>
<h3 id="&#x5728;-where-&#x4E2D;&#x4F7F;&#x7528;&#x63D2;&#x503C;">&#x5728; <code>where</code> &#x4E2D;&#x4F7F;&#x7528;&#x63D2;&#x503C;</h3>
<p>&#x4E3A;&#x4E86;&#x5728; where &#x8BED;&#x53E5;&#x4E2D;&#x4F7F;&#x7528; Elixir &#x8868;&#x8FBE;&#x5F0F;&#x6216;&#x8005;&#x63D2;&#x503C;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4F7F;&#x7528; <code>^</code>&#xFF0C;&#x6216;&#x8005;&#x53EB; pin &#x64CD;&#x4F5C;&#x7B26;&#x3002;&#x5B83;&#x5141;&#x8BB8;&#x6211;&#x4EEC;&#x628A;&#x4E00;&#x4E2A;&#x503C; <em>&#x9489;</em> &#x5230;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x4E0A;&#xFF0C;&#x907F;&#x514D;&#x91CD;&#x65B0;&#x7ED1;&#x5B9A;&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; title = <span class="hljs-string">&quot;Ready Player One&quot;</span>
<span class="hljs-string">&quot;Ready Player One&quot;</span>
iex&gt; query = from(m <span class="hljs-keyword">in</span> Movie, <span class="hljs-symbol">where:</span> m.title == ^title, <span class="hljs-symbol">select:</span> m.tagline)            
%Ecto.Query&lt;from m <span class="hljs-keyword">in</span> Example.Movie, <span class="hljs-symbol">where:</span> m.title == ^<span class="hljs-string">&quot;Ready Player One&quot;</span>,
 <span class="hljs-symbol">select:</span> m.tagline&gt;
iex&gt; Repo.all(query)

<span class="hljs-number">15</span><span class="hljs-symbol">:</span><span class="hljs-number">21</span><span class="hljs-symbol">:</span><span class="hljs-number">46.809</span> [debug] QUERY OK source=<span class="hljs-string">&quot;movies&quot;</span> db=<span class="hljs-number">3.8</span>ms
[<span class="hljs-string">&quot;Something about video games&quot;</span>]
</code></pre>
<h3 id="&#x83B7;&#x53D6;&#x9996;&#x6761;&#x6216;&#x672B;&#x5C3E;&#x8BB0;&#x5F55;">&#x83B7;&#x53D6;&#x9996;&#x6761;&#x6216;&#x672B;&#x5C3E;&#x8BB0;&#x5F55;</h3>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>Ecto.Query.first/2</code> &#x548C; <code>Ecto.Query.last/2</code> &#x51FD;&#x6570;&#x4ECE; repository &#x4E2D;&#x83B7;&#x53D6;&#x9996;&#x6761;&#x6216;&#x672B;&#x5C3E;&#x8BB0;&#x5F55;&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528; <code>first/2</code> &#x51FD;&#x6570;&#x6765;&#x5199;&#x67E5;&#x8BE2;&#x8868;&#x8FBE;&#x5F0F;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; first(Movie)
%Ecto.Query&lt;from m <span class="hljs-keyword">in</span> Example.Movie, <span class="hljs-symbol">order_by:</span> [<span class="hljs-symbol">desc:</span> m.id], <span class="hljs-symbol">limit:</span> <span class="hljs-number">1</span>&gt;
</code></pre>
<p>&#x7136;&#x540E;&#x6211;&#x4EEC;&#x628A;&#x8FD9;&#x4E2A;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x4F20;&#x7ED9; <code>Repo.one/2</code> &#x51FD;&#x6570;&#x6765;&#x83B7;&#x53D6;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; Movie |&gt; first() |&gt; Repo.one()

<span class="hljs-number">06</span><span class="hljs-symbol">:</span><span class="hljs-number">36</span><span class="hljs-symbol">:</span><span class="hljs-number">14.234</span> [debug] QUERY OK source=<span class="hljs-string">&quot;movies&quot;</span> db=<span class="hljs-number">3.7</span>ms
%Example.Movie{
  <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;movies&quot;</span>&gt;,
  <span class="hljs-symbol">actors:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:actors</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">characters:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:characters</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">distributor:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:distributor</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
  <span class="hljs-symbol">tagline:</span> <span class="hljs-string">&quot;Something about video games&quot;</span>,
  <span class="hljs-symbol">title:</span> <span class="hljs-string">&quot;Ready Player One&quot;</span>
}
</code></pre>
<p><code>Ecto.Query.last/2</code> &#x51FD;&#x6570;&#x7684;&#x4F7F;&#x7528;&#x65B9;&#x5F0F;&#x662F;&#x4E00;&#x6837;&#x7684;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; Movie |&gt; last() |&gt; Repo.one()
</code></pre>
<h2 id="&#x67E5;&#x8BE2;&#x5173;&#x8054;&#x6570;&#x636E;">&#x67E5;&#x8BE2;&#x5173;&#x8054;&#x6570;&#x636E;</h2>
<h3 id="&#x9884;&#x52A0;&#x8F7D;">&#x9884;&#x52A0;&#x8F7D;</h3>
<p>&#x4E3A;&#x4E86;&#x8BBF;&#x95EE;&#x90A3;&#x4E9B;&#x901A;&#x8FC7; <code>belongs_to</code>&#xFF0C;<code>has_many</code> &#x548C; <code>has_one</code> &#x5B8F;&#x66B4;&#x9732;&#x7ED9;&#x6211;&#x4EEC;&#x7684;&#x5173;&#x8054;&#x8BB0;&#x5F55;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981; <em>&#x9884;&#x52A0;&#x8F7D;</em> &#x60F3;&#x5173;&#x8054;&#x7684; schemas&#x3002;</p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x770B;&#x770B;&#x5F53;&#x5C1D;&#x8BD5;&#x83B7;&#x53D6;&#x7535;&#x5F71;&#x60F3;&#x5173;&#x8054;&#x7684;&#x89D2;&#x8272;&#x65F6;&#xFF0C;&#x60C5;&#x51B5;&#x4F1A;&#x600E;&#x4E48;&#x6837;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; movie = Repo.get(Movie, <span class="hljs-number">1</span>)
iex&gt; movie.actors
%Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:actors</span> is <span class="hljs-keyword">not</span> loaded&gt;
</code></pre>
<p>&#x6211;&#x4EEC; <em>&#x65E0;&#x6CD5;</em> &#x8BBF;&#x95EE;&#x90A3;&#x4E9B;&#x5173;&#x8054;&#x7684;&#x89D2;&#x8272;&#x9664;&#x975E;&#x6211;&#x4EEC;&#x9884;&#x52A0;&#x8F7D;&#x5B83;&#x4EEC;&#x3002;&#x6709;&#x51E0;&#x79CD;&#x4E0D;&#x540C;&#x7684;&#x4F7F;&#x7528; Ecto &#x9884;&#x52A0;&#x8F7D;&#x8BB0;&#x5F55;&#x7684;&#x65B9;&#x5F0F;&#x3002;</p>
<h4 id="&#x4F7F;&#x7528;&#x4E24;&#x6761;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x6765;&#x9884;&#x52A0;&#x8F7D;">&#x4F7F;&#x7528;&#x4E24;&#x6761;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x6765;&#x9884;&#x52A0;&#x8F7D;</h4>
<p>&#x4E0B;&#x9762;&#x7684;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x5C06;&#x4F7F;&#x7528;&#x53E6;&#x4E00;&#x6761; <em>&#x72EC;&#x7ACB;&#x7684;</em> &#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x6765;&#x9884;&#x52A0;&#x8F7D;&#x76F8;&#x5173;&#x8054;&#x7684;&#x8BB0;&#x5F55;&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; import Ecto.Query
Ecto.Query
iex&gt; Repo.all(from m <span class="hljs-keyword">in</span> Movie, <span class="hljs-symbol">preload:</span> [<span class="hljs-symbol">:actors</span>])
[
  %Example.Movie{
    <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;movies&quot;</span>&gt;,
    <span class="hljs-symbol">actors:</span> [
      %Example.Actor{
        <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;actors&quot;</span>&gt;,
        <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
        <span class="hljs-symbol">movies:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:movies</span> is <span class="hljs-keyword">not</span> loaded&gt;,
        <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Bob&quot;</span>
      },
      %Example.Actor{
        <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;actors&quot;</span>&gt;,
        <span class="hljs-symbol">id:</span> <span class="hljs-number">2</span>,
        <span class="hljs-symbol">movies:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:movies</span> is <span class="hljs-keyword">not</span> loaded&gt;,
        <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Gary&quot;</span>
      }
    ],
    <span class="hljs-symbol">characters:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:characters</span> is <span class="hljs-keyword">not</span> loaded&gt;,
    <span class="hljs-symbol">distributor:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:distributor</span> is <span class="hljs-keyword">not</span> loaded&gt;,
    <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
    <span class="hljs-symbol">tagline:</span> <span class="hljs-string">&quot;Something about video games&quot;</span>,
    <span class="hljs-symbol">title:</span> <span class="hljs-string">&quot;Ready Player One&quot;</span>
  }
]
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;&#x4E0A;&#x9762;&#x7684;&#x90A3;&#x884C;&#x4EE3;&#x7801;&#xFF0C;&#x8FD0;&#x884C;&#x4E86; <em>&#x4E24;&#x6761;</em> &#x6570;&#x636E;&#x5E93;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x3002;&#x4E00;&#x6761;&#x83B7;&#x53D6;&#x6240;&#x6709;&#x7684;&#x7535;&#x5F71;&#xFF0C;&#x53E6;&#x4E00;&#x6761;&#x83B7;&#x53D6;&#x4E86;&#x5404;&#x7535;&#x5F71; ID &#x76F8;&#x5173;&#x8054;&#x7684;&#x6240;&#x6709;&#x89D2;&#x8272;&#x3002;</p>
<h4 id="&#x4F7F;&#x7528;&#x4E00;&#x6761;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x6765;&#x9884;&#x52A0;&#x8F7D;">&#x4F7F;&#x7528;&#x4E00;&#x6761;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x6765;&#x9884;&#x52A0;&#x8F7D;</h4>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4EE5;&#x4E0B;&#x65B9;&#x5F0F;&#x6765;&#x51CF;&#x5C11;&#x6570;&#x636E;&#x5E93;&#x67E5;&#x8BE2;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; query = from(m <span class="hljs-keyword">in</span> Movie, <span class="hljs-symbol">join:</span> a <span class="hljs-keyword">in</span> assoc(m, <span class="hljs-symbol">:actors</span>), <span class="hljs-symbol">preload:</span> [<span class="hljs-symbol">actors:</span> a])
iex&gt; Repo.all(query)  
[
  %Example.Movie{
    <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;movies&quot;</span>&gt;,
    <span class="hljs-symbol">actors:</span> [
      %Example.Actor{
        <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;actors&quot;</span>&gt;,
        <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
        <span class="hljs-symbol">movies:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:movies</span> is <span class="hljs-keyword">not</span> loaded&gt;,
        <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Bob&quot;</span>
      },
      %Example.Actor{
        <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;actors&quot;</span>&gt;,
        <span class="hljs-symbol">id:</span> <span class="hljs-number">2</span>,
        <span class="hljs-symbol">movies:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:movies</span> is <span class="hljs-keyword">not</span> loaded&gt;,
        <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Gary&quot;</span>
      }
    ],
    <span class="hljs-symbol">characters:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:characters</span> is <span class="hljs-keyword">not</span> loaded&gt;,
    <span class="hljs-symbol">distributor:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:distributor</span> is <span class="hljs-keyword">not</span> loaded&gt;,
    <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
    <span class="hljs-symbol">tagline:</span> <span class="hljs-string">&quot;Something about video games&quot;</span>,
    <span class="hljs-symbol">title:</span> <span class="hljs-string">&quot;Ready Player One&quot;</span>
  }
]
</code></pre>
<p>&#x8FD9;&#x79CD;&#x505A;&#x6CD5;&#x5141;&#x8BB8;&#x6211;&#x4EEC;&#x53EA;&#x6267;&#x884C;&#x4E00;&#x6B21;&#x6570;&#x636E;&#x8C03;&#x7528;&#x3002;&#x5B83;&#x7684;&#x989D;&#x5916;&#x597D;&#x5904;&#x5C31;&#x662F;&#x53EF;&#x4EE5;&#x8BA9;&#x6211;&#x4EEC;&#x5728;&#x4E00;&#x6761;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x4E2D;&#x9009;&#x62E9;&#x8FC7;&#x6EE4;&#x7535;&#x5F71;&#x548C;&#x76F8;&#x5173;&#x7684;&#x89D2;&#x8272;&#x3002;&#x6BD4;&#x5982;&#x5B83;&#x53EF;&#x4EE5;&#x8BA9;&#x6211;&#x4EEC;&#x4F7F;&#x7528; <code>join</code> &#x8BED;&#x53E5;&#xFF0C;&#x67E5;&#x8BE2;&#x76F8;&#x5173;&#x89D2;&#x8272;&#x6EE1;&#x8DB3;&#x7279;&#x5B9A;&#x6761;&#x4EF6;&#x7684;&#x67D0;&#x4E00;&#x4E9B;&#x7535;&#x5F71;&#x3002;&#x6BD4;&#x5982;&#xFF1A;</p>
<pre><code class="lang-elixir">Repo.all from m <span class="hljs-keyword">in</span> Movie,
  <span class="hljs-symbol">join:</span> a <span class="hljs-keyword">in</span> assoc(m, <span class="hljs-symbol">:actors</span>),
  <span class="hljs-symbol">where:</span> a.name == <span class="hljs-string">&quot;John Wayne&quot;</span>
  <span class="hljs-symbol">preload:</span> [<span class="hljs-symbol">actors:</span> a]
</code></pre>
<p>&#x540E;&#x9762;&#x4F1A;&#x6709;&#x66F4;&#x591A;&#x5173;&#x4E8E; join &#x8BED;&#x53E5;&#x7684;&#x4ECB;&#x7ECD;&#x3002;</p>
<h4 id="&#x9884;&#x52A0;&#x8F7D;&#x5DF2;&#x7ECF;&#x67E5;&#x8BE2;&#x51FA;&#x6765;&#x7684;&#x8BB0;&#x5F55;&#x7684;&#x5173;&#x8054;&#x6570;&#x636E;">&#x9884;&#x52A0;&#x8F7D;&#x5DF2;&#x7ECF;&#x67E5;&#x8BE2;&#x51FA;&#x6765;&#x7684;&#x8BB0;&#x5F55;&#x7684;&#x5173;&#x8054;&#x6570;&#x636E;</h4>
<p>&#x6211;&#x4EEC;&#x8FD8;&#x53EF;&#x4EE5;&#x9884;&#x52A0;&#x8F7D;&#x90A3;&#x4E9B;&#x5DF2;&#x7ECF;&#x4ECE;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x67E5;&#x8BE2;&#x51FA;&#x6765;&#x7684;&#x8BB0;&#x5F55;&#x7684;&#x5173;&#x8054;&#x6570;&#x636E;&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; movie = Repo.get(Movie, <span class="hljs-number">1</span>)
%Example.Movie{
  <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;movies&quot;</span>&gt;,
  <span class="hljs-symbol">actors:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:actors</span> is <span class="hljs-keyword">not</span> loaded&gt;, <span class="hljs-comment"># actors are NOT LOADED!!</span>
  <span class="hljs-symbol">characters:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:characters</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">distributor:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:distributor</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
  <span class="hljs-symbol">tagline:</span> <span class="hljs-string">&quot;Something about video games&quot;</span>,
  <span class="hljs-symbol">title:</span> <span class="hljs-string">&quot;Ready Player One&quot;</span>
}
iex&gt; movie = Repo.preload(movie, <span class="hljs-symbol">:actors</span>)
%Example.Movie{
  <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;movies&quot;</span>&gt;,
  <span class="hljs-symbol">actors:</span> [
    %Example.Actor{
      <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;actors&quot;</span>&gt;,
      <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
      <span class="hljs-symbol">movies:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:movies</span> is <span class="hljs-keyword">not</span> loaded&gt;,
      <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Bob&quot;</span>
    },
    %Example.Actor{
      <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;actors&quot;</span>&gt;,
      <span class="hljs-symbol">id:</span> <span class="hljs-number">2</span>,
      <span class="hljs-symbol">movies:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:movies</span> is <span class="hljs-keyword">not</span> loaded&gt;,
      <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Gary&quot;</span>
    }
  ], <span class="hljs-comment"># actors are LOADED!!</span>
  <span class="hljs-symbol">characters:</span> [],
  <span class="hljs-symbol">distributor:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:distributor</span> is <span class="hljs-keyword">not</span> loaded&gt;,
  <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
  <span class="hljs-symbol">tagline:</span> <span class="hljs-string">&quot;Something about video games&quot;</span>,
  <span class="hljs-symbol">title:</span> <span class="hljs-string">&quot;Ready Player One&quot;</span>
}
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x4ECE;&#x8FD9;&#x4E2A;&#x7535;&#x5F71;&#x4E2D;&#x83B7;&#x53D6;&#x5B83;&#x7684;&#x89D2;&#x8272;&#x4E86;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; movie.actors
[
  %Example.Actor{
    <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;actors&quot;</span>&gt;,
    <span class="hljs-symbol">id:</span> <span class="hljs-number">1</span>,
    <span class="hljs-symbol">movies:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:movies</span> is <span class="hljs-keyword">not</span> loaded&gt;,
    <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Bob&quot;</span>
  },
  %Example.Actor{
    <span class="hljs-symbol">__meta__:</span> %Ecto.Schema.Metadata&lt;<span class="hljs-symbol">:loaded</span>, <span class="hljs-string">&quot;actors&quot;</span>&gt;,
    <span class="hljs-symbol">id:</span> <span class="hljs-number">2</span>,
    <span class="hljs-symbol">movies:</span> %Ecto.Association.NotLoaded&lt;association <span class="hljs-symbol">:movies</span> is <span class="hljs-keyword">not</span> loaded&gt;,
    <span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Gary&quot;</span>
  }
]
</code></pre>
<h3 id="&#x4F7F;&#x7528;-join-&#x8BED;&#x53E5;">&#x4F7F;&#x7528; Join &#x8BED;&#x53E5;</h3>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; <code>Ecto.Query.join/5</code> &#x51FD;&#x6570;&#x7684;&#x5E2E;&#x52A9;&#x6765;&#x6267;&#x884C;&#x5305;&#x542B; join &#x8BED;&#x53E5;&#x7684;&#x67E5;&#x8BE2;&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; query = from m <span class="hljs-keyword">in</span> Movie,
              <span class="hljs-symbol">join:</span> c <span class="hljs-keyword">in</span> Character,
              <span class="hljs-symbol">on:</span> m.id == c.movie_id,
              <span class="hljs-symbol">where:</span> c.name == <span class="hljs-string">&quot;Video Game Guy&quot;</span>,
              <span class="hljs-symbol">select:</span> {m.title, c.name}
iex&gt; Repo.all(query)
<span class="hljs-number">15</span><span class="hljs-symbol">:</span><span class="hljs-number">28</span><span class="hljs-symbol">:</span><span class="hljs-number">23.756</span> [debug] QUERY OK source=<span class="hljs-string">&quot;movies&quot;</span> db=<span class="hljs-number">5.5</span>ms
[{<span class="hljs-string">&quot;Ready Player One&quot;</span>, <span class="hljs-string">&quot;Video Game Guy&quot;</span>}]
</code></pre>
<p><code>on</code> &#x8868;&#x8FBE;&#x5F0F;&#x4E5F;&#x53EF;&#x4EE5;&#x63A5;&#x6536;&#x4E00;&#x4E2A;&#x5173;&#x952E;&#x5B57;&#x5217;&#x8868;&#xFF1A;</p>
<pre><code class="lang-elixir">from m <span class="hljs-keyword">in</span> Movie,
  <span class="hljs-symbol">join:</span> c <span class="hljs-keyword">in</span> Character,
  <span class="hljs-symbol">on:</span> [<span class="hljs-symbol">id:</span> c.movie_id], <span class="hljs-comment"># keyword list</span>
  <span class="hljs-symbol">where:</span> c.name == <span class="hljs-string">&quot;Video Game Guy&quot;</span>,
  <span class="hljs-symbol">select:</span> {m.title, c.name}
</code></pre>
<p>&#x5728;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x6211;&#x4EEC;&#x8FDE;&#x63A5;&#x4E86; Ecto schema&#xFF0C;<code>m in Movie</code>&#x3002;&#x6211;&#x4EEC;&#x5176;&#x5B9E;&#x4E5F;&#x53EF;&#x4EE5;&#x8FDE;&#x63A5;&#x4E00;&#x4E2A; Ecto &#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x3002;&#x5047;&#x8BBE;&#x8BF4;&#x6211;&#x4EEC;&#x7684;&#x7535;&#x5F71;&#x8868;&#x683C;&#x6709;&#x4E00;&#x4E2A; <code>stars</code> &#x5B57;&#x6BB5;&#x3002;&#x90A3;&#x91CC;&#x4FDD;&#x5B58;&#x7684;&#x662F;&#x4E00;&#x90E8;&#x7535;&#x5F71;&#x7684;&#x201C;&#x8BC4;&#x4EF7;&#x201D;&#xFF0C;&#x6570;&#x5B57; 1 &#x81F3; 5&#x3002;</p>
<pre><code class="lang-elixir">movies = from m <span class="hljs-keyword">in</span> Movie, <span class="hljs-symbol">where:</span> [<span class="hljs-symbol">stars:</span> <span class="hljs-number">5</span>]
from c <span class="hljs-keyword">in</span> Character,
  <span class="hljs-symbol">join:</span> ^movies,
  <span class="hljs-symbol">on:</span> [<span class="hljs-symbol">id:</span> c.movie_id], <span class="hljs-comment"># keyword list</span>
  <span class="hljs-symbol">where:</span> c.name == <span class="hljs-string">&quot;Video Game Guy&quot;</span>,
  <span class="hljs-symbol">select:</span> {m.title, c.name}
</code></pre>
<p>Ecto &#x67E5;&#x8BE2;&#x8BED;&#x53E5; DSL &#x662F;&#x4E00;&#x4E2A;&#x5F3A;&#x5927;&#x7684;&#x5DE5;&#x5177;&#x3002;&#x5B83;&#x63D0;&#x4F9B;&#x4E86;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x7684;&#x6240;&#x6709;&#x7528;&#x4E8E;&#x751F;&#x6210;&#x590D;&#x6742;&#x6570;&#x636E;&#x5E93;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x7684;&#x4E1C;&#x897F;&#x3002;&#x901A;&#x8FC7;&#x672C;&#x6B21;&#x4ECB;&#x7ECD;&#xFF0C;&#x5E0C;&#x671B;&#x4F60;&#x80FD;&#x5177;&#x5907;&#x5F00;&#x59CB;&#x67E5;&#x8BE2;&#x7684;&#x57FA;&#x672C;&#x77E5;&#x8BC6;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="associations.html" class="navigation navigation-prev " aria-label="Previous page: 关联关系">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../specifics/plug.html" class="navigation navigation-next " aria-label="Next page: Plug">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.0.2","title":"查询","level":"3.4","depth":1,"next":{"title":"Plug","level":"4.1","depth":1,"path":"lessons/specifics/plug.md","ref":"lessons/specifics/plug.md","articles":[]},"previous":{"title":"关联关系","level":"3.3","depth":1,"path":"lessons/ecto/associations.md","ref":"lessons/ecto/associations.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/ecto/querying.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

